//
//  SelectExamTimeSignUpView.m
//  CommunityNannyO2O
//
//  Created by 90 on 15/7/21.
//  Copyright (c) 2015年 Apple. All rights reserved.
//

#import "SelectExamTimeSignUpView.h"
#import "AppConfig.h"
#import "QXNetWorkTool.h"
#import "APIAdress.h"
#import "MBProgressHUD+YC.h"
#import "MJExtension.h"
#import "AccountTool.h"
#import "CreateSignUpTableController.h"
#import "SelectExamTimeSignUpCell.h"
#import "SelectExamTiemModel.h"
#import "UserDataSingleton.h"

@interface SelectExamTimeSignUpView()<QXNetWorkToolDelegate,
        UITableViewDataSource,UITableViewDelegate,
        UIPickerViewDataSource, UIPickerViewDelegate>
{

}

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (copy, nonatomic) NSString *selectStartHour;
@property (copy, nonatomic) NSString *selectStartMinute;
@property (copy, nonatomic) NSString *selectEndHour;
@property (copy, nonatomic) NSString *selectEndMinute;
@property (copy, nonatomic) NSString *timeSeiconId;

@property (weak, nonatomic) IBOutlet UIButton *confirm;

@end


@implementation SelectExamTimeSignUpView


@synthesize timeData;

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.tableView.dataSource = self;
    
    self.tableView.delegate = self;
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    self.confirm.layer.cornerRadius = 5;
    self.confirm.layer.masksToBounds = YES;
}


//tableview
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.timeData count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 71;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SelectExamTimeSignUpCell *cell = [SelectExamTimeSignUpCell cellWithTableView:tableView];
    
    SelectExamTiemModel *model = [self.timeData objectAtIndex:indexPath.row];
    NSString *timeStr = model.exam_time;
    
    NSString *hour = [timeStr substringWithRange:NSMakeRange(0, 2)];
    NSString *minute = [timeStr substringWithRange:NSMakeRange(3, 2)];
    
    //每个时间段
    NSMutableArray *eachTime = [self getTimeByStartHour:[hour intValue]
                                            StartMinute:[minute intValue]
                                               Interval:20];
    
    [cell setTimeStartHour:[eachTime objectAtIndex:0]
               StartMinute:[eachTime objectAtIndex:1]
                   EndHour:[eachTime objectAtIndex:2]
                 EndMinute:[eachTime objectAtIndex:3]];
    
    NSString *tmpShour = cell.startHour;
    NSString *tmpSminute = cell.startMinute;
    NSString *tmpEhour = cell.endHour;
    NSString *tmpEminute = cell.endMinute;
    
    if ([tmpShour intValue] < 10)
    {
        tmpShour = [NSString stringWithFormat:@"0%@", tmpShour];
    }
    
    if ([tmpSminute intValue] < 10)
    {
        tmpSminute = [NSString stringWithFormat:@"0%@", tmpSminute];
    }
    
    if ([tmpEhour intValue] < 10)
    {
        tmpEhour = [NSString stringWithFormat:@"0%@", tmpEhour];
    }
    
    if ([tmpEminute intValue] < 10)
    {
        tmpEminute = [NSString stringWithFormat:@"0%@", tmpEminute];
    }
    
    cell.time.text = [NSString stringWithFormat:@"%@:%@ ~ %@:%@", tmpShour,
                      tmpSminute, tmpEhour, tmpEminute];
    
    cell.timeId = model.id;
    return cell;
}

- (NSMutableArray *)getTimeByStartHour:(int)startHour
                           StartMinute:(int)startMinute
                              Interval:(int)interval
{
    int curHo = startHour;
    int curM = startMinute;
    
    int sH = curHo;
    int sM = curM;
    
    curM += interval;
    if (curM >= 60)
    {
        ++curHo;
        curM = curM - 60;
    }
    
    NSMutableArray * timeAry = [[NSMutableArray alloc] init];
    
    [timeAry addObject:[NSString stringWithFormat:@"%d", sH]];
    [timeAry addObject:[NSString stringWithFormat:@"%d", sM]];
    [timeAry addObject:[NSString stringWithFormat:@"%d", curHo]];
    [timeAry addObject:[NSString stringWithFormat:@"%d", curM]];
    
    return timeAry;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSArray *array = [tableView visibleCells];
    
    for (SelectExamTimeSignUpCell *cell in array)
    {
        cell.check.hidden = YES;
    }
    
    SelectExamTimeSignUpCell *cell = (SelectExamTimeSignUpCell *)[tableView cellForRowAtIndexPath:indexPath];
    
    self.selectStartHour = cell.startHour;
    self.selectStartMinute = cell.startMinute;
    self.selectEndHour = cell.endHour;
    self.selectEndMinute = cell.endMinute;
    self.timeSeiconId = cell.timeId;
    
    cell.check.hidden = NO;
}

-(IBAction)finish:(id)sender
{
    [self finishSHour:self.selectStartHour
              SMinute:self.selectStartMinute
                EHour:self.selectEndHour
              EMinute:self.selectEndMinute
         TimeSeiconId:self.timeSeiconId];
}

-(void)finishSHour:(NSString*)sHour SMinute:(NSString*)sMinute
             EHour:(NSString*)eHour EMinute:(NSString*)eMinute
      TimeSeiconId:(NSString*)timeSecionId;
{
    if (!sHour || !sMinute || !eHour || !eMinute)
    {
        [MBProgressHUD showError:@"请选择考试时间"];
        return;
    }
    
    CreateSignUpTableController *obj = [CreateSignUpTableController create];
    
    UserDataSingleton *userData = [UserDataSingleton shareInstance];
    userData.sHour = sHour;
    userData.sMinute = sMinute;
    userData.eHour = eHour;
    userData.eMinute = eMinute;
    userData.timeSecionId = timeSecionId;
    
    [self.navigationController pushViewController:obj animated:YES];
}

-(void)configNavigationItem
{
    self.navigationItem.title = @"报名";
    [self.navigationController.navigationBar setTranslucent:NO];
    
    [self.navigationController.navigationBar
     setBackgroundImage:[UIImage imageNamed:@"bg_navigation_bar"]
     forBarMetrics:UIBarMetricsDefault];
    
    //status bar的字体为白色
    //导航栏的背景色是黑色
    self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
    
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],NSForegroundColorAttributeName, [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1.0],UITextAttributeTextShadowColor,[UIFont fontWithName:@"Hiragino Sans GB" size:18.0],NSFontAttributeName,nil]];
    
    [self configNavigationBackAction:^{
        
        [self.navigationController popViewControllerAnimated:YES];
    }];
}

-(void)useMethodToFindBlackLineAndHind
{
    UIImageView* blackLineImageView = [self findHairlineImageViewUnder:self.navigationController.navigationBar];
    
    blackLineImageView.hidden = YES;
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    [self configNavigationItem];

    [self useMethodToFindBlackLineAndHind];
}

@end









